using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bussiness;
using Entity;

namespace UI
{
    public partial class NewFile : Form
    {
        private string server;
        private string user;
        private string pass;
        public NewFile()
        {
            InitializeComponent();
            //Ftp Server
            server = ManageSetting.GetDefineByType(CDefine.FTPServer)[0].memo;
            //Ftp User and password
            string tmp = ManageSetting.GetDefineByType(CDefine.FTPUser)[0].memo;
            if (tmp != null)
            {
                string[] obj = tmp.Split(' ');
                if(obj.Length ==1)
                    user = obj[0];
                else if (obj.Length == 2)
                {
                    user = obj[0];
                    pass = obj[1];
                }
            }
        }

        private void btn_OpenFile_Click(object sender, EventArgs e)
        {
            this.oFD_File.ShowDialog();
            this.txt_File.Text = oFD_File.FileName;
        }

        private void btn_Upload_Click(object sender, EventArgs e)
        {
            string filepath;
            int projectid;
            bool isRoot= true;
            if (this.tv_Project.SelectedNode.Parent != null)
            {
                filepath = this.tv_Project.SelectedNode.Parent.Text + "/" + this.tv_Project.SelectedNode.Text + "/" + this.cbB_Type.SelectedItem.ToString();
                isRoot = false;
               
            }
            else
                filepath = this.tv_Project.SelectedNode.Text + "/" + this.cbB_Type.SelectedItem.ToString();
            projectid = (int)this.tv_Project.SelectedNode.Tag;
            //MessageBox.Show(s);
            FTPHelper.UpLoad(this.txt_File.Text,server,filepath, user, pass);
            FileInfo fileInf = new FileInfo(this.oFD_File.FileName);
            
            Entity.File f = new Entity.File();
            f.name = fileInf.Name;
            f.path = filepath;
            f.flag = isRoot;
            f.projectid = projectid;

            FileManage.Add(f);
            this.Close();
        }

        private void NewFile_Load(object sender, EventArgs e)
        {
            List<Project> plist = ProjectManage.GetProjectAll();

            foreach (Project p in plist)
            {
                TreeNode node = new TreeNode();
                node.Text = p.name;
                node.Tag = p.Id;
                List<SubProject> sp = ProjectManage.GetSubProject(p.Id);
                foreach (SubProject s in sp)
                {
                    TreeNode snode = new TreeNode();
                    snode.Text = s.name;
                    snode.Tag = s.Id;
                    node.Nodes.Add(snode);
                }
                this.tv_Project.Nodes.Add(node);
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}